#include "stdafx.h"
#include "appviewimpl.h"

int main_t(_pinstance hInstance, int argc, TCHAR* argv[])
{
	logi("s_strcpy_func %p\n", s_strcpy);
	logi("s_strcpy_func %p\n", s_strcat);

	HRESULT hr = S_OK;

    int iRes = S_SUCCESS;

    basic_tchar szPath[MAX_PATH + 1] = { 0x00 };
    GetExecutePath(INSNULL, szPath, MAX_PATH);
    size_t pathlen = tstring_strlen(szPath);
  
    CContainer com;


#if (TARGET_OS == OS_WINDOWS)

	const char* pCode = "{\"component\":[ \
        \"{F170A724-AACA-4603-BB1C-0A8EAF1C3322}:asynio:1:4\",\
        \"{C54C9CC0-F448-4A49-A622-0467D02E8EB8}:net:2:4\",\
	    \"{99103D46-735F-44EE-A6F1-2C94DF20901E}:logs:5:4\",\
        \"{34F9B3BF-6F56-4058-9DCD-04D9A5F0174B}:logs:4:4\",\
        \"{A8B9F69C-3523-406A-831B-2416421F324E}:logs:6:4\",\
		\"{51FA8DE1-216F-4A76-B4F4-B986E9F54C27}:mainview:100:4\",\
		\"{CE065148-7803-45B1-B1D7-874B52B8F5C5}:uibase:10:4\",\
        \"{201409F6-22F8-48d3-A69F-7935BDDE6BFF}:msgbus:10:4\" \
        ]}";
#else 
    const char* pCode = "{\"component\":[ \
        \"{F170A724-AACA-4603-BB1C-0A8EAF1C3322}:asynio:1:4\",\
        \"{C54C9CC0-F448-4A49-A622-0467D02E8EB8}:net:2:4\",\
	    \"{99103D46-735F-44EE-A6F1-2C94DF20901E}:logs:5:4\",\
        \"{34F9B3BF-6F56-4058-9DCD-04D9A5F0174B}:logs:4:4\",\
        \"{A8B9F69C-3523-406A-831B-2416421F324E}:logs:6:4\",\
        \"{201409F6-22F8-48d3-A69F-7935BDDE6BFF}:msgbus:10:4\" \
        ]}";

#endif


    logi("test_main_CreateContainer");
	hr = com.CreateContainer(szPath, pathlen);

    logi("test_main_RegisterContainer");
	hr = com.RegisterContainer(pCode, s_strlen(pCode));

    logi("test_main_InitContainer");
	hr = com.InitContainer(hInstance, argv, argc);

	ULONG count = com.GetCompentCount();
	ULONG Index = com.GetStartIndex(count - 1);

	CAppViewImpl *app = ALLOC_NEW CAppViewImpl();
	BasePtr pBase;
	hr = app->QueryInterface(IID_IBase, (void**)&pBase);
	

	if (hr == S_OK) {
		hr = com.RegisterComponent(CLSID_AppView, "AppView.impl.V1", pBase, Index - 1, "tesemain", STD_COMPOENT | STD_INIT);
	}

	logi("test_main_RunContainer");
	hr = com.RunContainer(hInstance, argv, argc, 0);

	logi("test_main_InitContainer");
	hr = com.UnInitContainer(hInstance);

    logi("test_main_DestroyContainer");

    return 0;
}

#if (TARGET_OS == OS_POSIX)

int main(int argc, char* argv[])
{
	_pinstance hInstance = getpid();
    return main_t(hInstance, argc, argv);
}

#elif (TARGET_OS == OS_DARWIN)

int main(int argc, char* argv[])
{
	_pinstance hInstance = getpid();
    return main_t(hInstance, argc, argv);
}

#elif (TARGET_OS == OS_WINDOWS)

int WINAPI WinMain(HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int nCmdShow) {

    int iRet = 0;
    int nArgs = 0;
    LPWSTR* szArglist = NULL;
    szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);
    iRet = main_t(hInstance, nArgs, szArglist);
    rc_assert(nArgs != 0, 0)
    LocalFree(szArglist);
    return 0;
}
#endif
